Developer --> Technical Publications
PATHJava Developer Documentation > Mac OS Runtime for Java > JManager > Programming With JManager


Handling Events

Although the Java applet runs within its own runtime environment, it must often interact with events that occur in the Mac OS. For example, user events (such as mouse clicks) or system events may require some response from the Java program. This section describes JManager functions for handling such events. For more information about how the Mac OS handles events, see "Event Manager" in Inside Macintosh: Macintosh Toolbox Essentials.

Actions performed on a frame may need to be reflected on the user-visible window as well, but in most cases, such actions are handled by JManager through a callback. For example, if the user clicks on an applet window's close box, your application must notify the frame that this has occurred. The Java program can then take action based upon this event, such as removing the frame or displaying a message in a dialog box. In either case, the AWT uses the callback functions you defined in your application to display the results. Figure 1-5 shows the steps for removing an applet window.

Figure 1-5 Removing an applet window

See Displaying Frames for more information about using the callbacks.

When an event occurs, you typically call an event-handling function from your main loop. Listing 1-12 shows an example of an event-handling function.

Listing 1-12 Handling events

static void handleEvent(const EventRecord* eve)
{
    switch (eve->what) {

        case updateEvt:
            handleUpdate((WindowPtr) eve->message);
            break;
            
        case activateEvt:
            handleActivate((eve->modifiers & activeFlag) != 0,
                (WindowPtr) eve->message);
            break;

        case osEvt:
            /* everyone should care about this */
            handleResume((eve->message & resumeFlag) != 0);
            break;

        case kHighLevelEvent:
            AEProcessAppleEvent(eve);
            break;

        case mouseDown:
            handleMouse(eve);
            break;

        /* assume no one cares about these */
        case mouseUp:
            break;

        case keyDown:
        case autoKey:
        case keyUp:
            handleKey(eve);
            break;
    }

}

High-level events (generally Apple events) are handled by calling the Mac OS Toolbox function AEProcessAppleEvent . In other cases, the handling functions should check to see if the event needs to be passed to the embedded Java program. The sections that follow describe the JManager functions needed to pass events and give sample implementations of the handleUpdate , handleActivate , handleResume , handleMouse , and handleKey functions.


© 1998 Apple Computer, Inc. — (Last Updated 3 Dec 98)

Previous | Back Up One Level | Next |